﻿using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Service;
using AdverseEventReportSystem.Utils;
using FluentData;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Controllers
{
    [LoginAttribute(IsNeed = false)]
    public class UpdateDbController : BaseController
    {
        // GET: UpdateDb
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult UpdateAdverseEventDb()
        {
            OperateResult resultInfo = new OperateResult();
            string sql = "";
            try
            {
                string departmentCode = WebUtils.GetString("departmentCode");

                using (IDbContext context = Db.GetInstance().Context())
                {
                    context.UseTransaction(true);
                    sql = "Select Count(1) From AdverseEvent where AreaID = '" + departmentCode + "'";
                    if (BaseService<Model.AdverseEvent>.GetCount(sql, context) > 0)
                    {
                        resultInfo.Result = 0;
                        resultInfo.Body = "该病区已经初始化！";
                        return JsonDate(resultInfo);
                    }

                    ///查询不良事件表
                    sql = "Select * From V_AdverseEvent where DepartId = '" + departmentCode + "' Order By Id";
                    DataTable eventDt = BaseService<Model.AdverseEvent>.GetDataTable(sql, context);

                    ///查询事件发生方式表
                    sql = "Select * From V_AdverseEventModeRecord";
                    DataTable eventModeDt = BaseService<Model.AdverseEventModeRecord>.GetDataTable(sql, context);

                    ///查询事件根因表
                    sql = "Select distinct AdverseEventId,reasonId,reason From V_AdverseEventReasonRecord";
                    DataTable eventReasonDt = BaseService<Model.AdverseEventReasonRecord>.GetDataTable(sql, context);

                    ///查询事件当事人表
                    sql = "Select * From V_AdverseEventNurse";
                    DataTable eventNurseDt = BaseService<Model.AdverseEventNurse>.GetDataTable(sql, context);

                    ///查询压疮发生部位表
                    sql = "Select * From V_AdverseEventYaChuangFsbw";
                    DataTable yaChuangFsbwDt = BaseService<Model.AdverseEventYaChuangFsbw>.GetDataTable(sql, context);

                    ///查询不良事件类型发生方式
                    sql = "Select * From AdverseEventMode";
                    DataTable modeDt = BaseService<Model.AdverseEventMode>.GetDataTable(sql, context);

                    ///查询不良事件类型根因
                    sql = "Select * From AdverseEventReason";
                    DataTable reasonDt = BaseService<Model.AdverseEventReason>.GetDataTable(sql, context);

                    ///循环不良事件
                    foreach (DataRow eventDr in eventDt.Rows)
                    {
                        string eventTypeName = "";
                        AdverseEventType adverseEventType = AdverseEventTypeService.GetQuerySingleByID(DataConvert.StrToInt(eventDr["EventTypeId"]));
                        if (adverseEventType != null)
                        {
                            eventTypeName = adverseEventType.Name;
                        }

                        ///解析年龄
                        string strPatientAge = eventDr["PatientAge"].ToString();
                        int patientAge = 0;
                        int PatientAgeMonth = 0;
                        int PatientAgeWeek = 0;
                        int PatientAgeDay = 0;
                        if (!string.IsNullOrWhiteSpace(strPatientAge))
                        {
                            //提取数字
                            MatchCollection sz = Regex.Matches(strPatientAge, @"\d+");
                            //提取中文
                            MatchCollection zw = Regex.Matches(strPatientAge, @"\D+");

                            for (int i = 0; i < zw.Count; i++)
                            {
                                if (zw[i].Value == "岁")
                                {
                                    patientAge = DataConvert.StrToInt(sz[i].Value);
                                }
                                else if (zw[i].Value == "月")
                                {
                                    PatientAgeMonth = DataConvert.StrToInt(sz[i].Value);
                                }
                                else if (zw[i].Value == "周")
                                {
                                    PatientAgeWeek = DataConvert.StrToInt(sz[i].Value);
                                }
                                else if (zw[i].Value == "天")
                                {
                                    PatientAgeDay = DataConvert.StrToInt(sz[i].Value);
                                }
                            }
                        }

                        ///陪护情况,多个用#隔开
                        string patientEscortValue = "";
                        foreach (char c in eventDr["EscortType"].ToString().ToCharArray())
                        {
                            patientEscortValue += c + "#";
                        }
                        patientEscortValue = patientEscortValue.TrimEnd('#');

                        string skinCondition = "";
                        string exceptionalCase = "";
                        string duDaoRemark = "";
                        if (DataConvert.StrToInt(eventDr["flag_yc"]) == 1)
                        {
                            skinCondition = eventDr["EventCourse"].ToString();
                            exceptionalCase = eventDr["Measures"].ToString();
                            duDaoRemark = eventDr["Evaluate"].ToString();
                        }

                        ///合并旧数据的状态
                        int status = 0;
                        if (DataConvert.StrToInt(eventDr["ReviewState"]) == 0)
                        {
                            if (DataConvert.StrToInt(eventDr["ReportState"]) != 0)
                                status = 1;
                        }
                        else
                        {
                            switch (DataConvert.StrToInt(eventDr["ReviewState"]))
                            {
                                case 1:
                                    status = 2;
                                    break;
                                case 2:
                                    status = 3;
                                    break;
                                case 3:
                                    status = 4;
                                    break;
                                case 4:
                                    status = 5;
                                    break;
                                default:
                                    break;
                            }
                        }

                        #region 添加不良事件SQL语句

                        sql = @"set identity_insert AdverseEvent ON 
                                INSERT INTO [AdverseEventReport].[dbo].[AdverseEvent]
                                           ([ID]
                                           ,[EventTypeID]
                                           ,[EventTypeName]
                                           ,[AreaID]
                                           ,[AreaName]
                                           ,[PatientID]
                                           ,[PatientName]
                                           ,[PatientSex]
                                           ,[PatientAge]
                                           ,[PatientAgeMonth]
                                           ,[PatientAgeWeek]
                                           ,[PatientAgeDay]
                                           ,[PatientNursingGrade]
                                           ,[PatientIsEscort]
                                           ,[PatientEscortValue]
                                           ,[PatientDiagnosis]
                                           ,[PatientInTime]
                                           ,[PatientBedNo]
                                           ,[PatientVisitID]
                                           ,[OccurrTime]
                                           ,[NurseID]
                                           ,[NurseName]
                                           ,[NurseWorkTimes]
                                           ,[EventGrade]
                                           ,[EventCourse]
                                           ,[Measures]
                                           ,[Evaluation]
                                           ,[Status]
                                           ,[ReviewTime]
                                           ,[ReviewUserID]
                                           ,[ReviewReason]
                                           ,[OccurrDepartID]
                                           ,[OccurrDepartName]
                                           ,[SkinCondition]
                                           ,[ExceptionalCase]
                                           ,[DuDaoRemark]
                                           ,[YuHou]
                                           ,[ZhuanGui]
                                           ,[Flag_Yc]
                                           ,[FanKuiState]
                                           ,[FanKuiReason]
                                           ,[UpdateUserID]
                                           ,[UpdateTime]
                                           ,[CreateUserID]
                                           ,[CreateTime])
                                VALUES (" +
                                       eventDr["Id"] + "," +
                                       eventDr["EventTypeId"] + "," +
                                       "'" + eventTypeName + "'," +
                                       "'" + eventDr["DepartId"] + "'," +
                                       "'" + eventDr["DepartName"] + "'," +
                                       "'" + eventDr["PatientID"] + "'," +
                                       "'" + eventDr["PatientName"] + "'," +
                                       "'" + eventDr["PatientSex"] + "'," +
                                       patientAge + "," +
                                       PatientAgeMonth + "," +
                                       PatientAgeWeek + "," +
                                       PatientAgeDay + "," +
                                       "'" + eventDr["Nursing"] + "'," +
                                       eventDr["IsEscort"] + "," +
                                       "'" + patientEscortValue + "'," +
                                       "'" + eventDr["Diagnosis"] + "'," +
                                       "'" + eventDr["PatientInDate"] + "'," +
                                       "'" + eventDr["BedNo"] + "'," +
                                       eventDr["VisitID"] + "," +
                                       "'" + eventDr["OccurrTime"] + "'," +
                                       "'" + eventDr["NurseID"] + "'," +
                                       "'" + eventDr["NurseName"] + "'," +
                                       "'" + eventDr["WorkTimes"] + "'," +
                                       "'" + eventDr["EventLevel"] + "'," +
                                       "'" + eventDr["EventCourse"] + "'," +
                                       "'" + eventDr["Measures"] + "'," +
                                       "'" + eventDr["Evaluate"] + "'," +
                                       status + "," +
                                       "'" + eventDr["ReviewTime"] + "'," +
                                       "'" + eventDr["ReviewUser"] + "'," +
                                       "'" + eventDr["Reviewreason"] + "'," +
                                       "'" + eventDr["OccurrDepartID"] + "'," +
                                       "'" + eventDr["OccurrDepartName"] + "'," +
                                       "'" + skinCondition + "'," +
                                       "'" + exceptionalCase + "'," +
                                       "'" + duDaoRemark + "'," +
                                       "'" + eventDr["YuHou"] + "'," +
                                       "'" + eventDr["ZhuanGui"] + "'," +
                                       "'" + DataConvert.StrToInt(eventDr["flag_yc"], 0) + "'," +
                                       "'" + eventDr["FanKuiState"] + "'," +
                                       "'" + eventDr["FanKuiReason"] + "'," +
                                       "'" + eventDr["UpdateUserID"] + "'," +
                                       "'" + eventDr["UpdateTime"] + "'," +
                                       "'" + eventDr["AddUserId"] + "'," +
                                       "'" + eventDr["AddTime"] + "'" +
                                ")";

                        #endregion

                        int eventId = context.Sql(sql).ExecuteReturnLastId<int>();
                        if (eventId == 0)
                        {
                            resultInfo.Result = 0;
                            break;
                        }

                        if (resultInfo.Result == 1)
                        {
                            #region 添加发生方式

                            ///获取旧表的事件发生方式
                            DataRow[] eventModeDrs = eventModeDt.Select("AdverseEventId = " + eventId);
                            foreach (var dr in eventModeDrs)
                            {
                                if (DataConvert.StrToInt(dr["modeid"]) != 0)
                                {
                                    ///根据旧表数据获取新表对应的发生方式
                                    DataRow modeDr = modeDt.Select("ID = " + dr["modeid"])[0];
                                    AdverseEventModeRecord adverseEventModeRecord = new AdverseEventModeRecord();
                                    adverseEventModeRecord.EventID = DataConvert.StrToInt(dr["AdverseEventId"]);
                                    adverseEventModeRecord.TypeID = DataConvert.StrToInt(eventDr["EventTypeId"]);
                                    adverseEventModeRecord.ModeID = DataConvert.StrToInt(dr["modeid"]);
                                    adverseEventModeRecord.ModeName = modeDr["ModeName"].ToString();
                                    adverseEventModeRecord.InputType = DataConvert.StrToInt(modeDr["InputType"]);
                                    adverseEventModeRecord.Options = modeDr["Options"].ToString();
                                    string value = "";
                                    if (DataConvert.StrToInt(modeDr["InputType"]) == 0 || DataConvert.StrToInt(modeDr["InputType"]) == 1)
                                    {
                                        string[] oldValue = dr["mode"].ToString().Split('#');
                                        foreach (var op in modeDr["Options"].ToString().Split(new string[] { "$0%" }, StringSplitOptions.None)[0].Split('#'))
                                        {
                                            if (modeDr["ModeName"].ToString() == "感觉" || modeDr["ModeName"].ToString() == "潮湿" || modeDr["ModeName"].ToString() == "活动能力" || modeDr["ModeName"].ToString() == "移动能力" || modeDr["ModeName"].ToString() == "营养" || modeDr["ModeName"].ToString() == "摩擦力")
                                            {
                                                if (oldValue.Contains(op.Split('☆')[0]))
                                                {
                                                    value += "1#";
                                                }
                                                else
                                                {
                                                    value += "0#";
                                                }
                                            }
                                            else
                                            {
                                                if (oldValue.Contains(op))
                                                {
                                                    value += "1#";
                                                }
                                                else
                                                {
                                                    value += "0#";
                                                }
                                            }
                                        }
                                        value = value.TrimEnd('#');
                                    }
                                    else
                                    {
                                        value = dr["mode"].ToString();
                                    }

                                    if (modeDr["Options"].ToString().Contains("$0%"))
                                    {
                                        if (modeDr["Options"].ToString().Contains("$0%0*"))
                                        {
                                            value += "$0%0*";
                                            string[] tempOptions = modeDr["Options"].ToString().Split(new string[] { "$0%0*" }, StringSplitOptions.None)[1].Split('@');
                                            string childMode = "";
                                            if (eventModeDt.Select("AdverseEventId = " + eventId + " and modeid='child" + dr["modeid"] + "'").Length > 0)
                                            {
                                                childMode = eventModeDt.Select("AdverseEventId = " + eventId + " and modeid='child" + dr["modeid"] + "'")[0]["mode"].ToString();
                                            }
                                            foreach (var tempOp in tempOptions)
                                            {
                                                if (childMode.Contains(tempOp))
                                                {
                                                    value += "1@";
                                                }
                                                else
                                                {
                                                    value += "0@";
                                                }
                                            }
                                        }
                                        else if (modeDr["Options"].ToString().Contains("$0%1*"))
                                        {
                                            value += "$0%1*";
                                            string[] tempOptions = modeDr["Options"].ToString().Split(new string[] { "$0%1*" }, StringSplitOptions.None)[1].Split('@');
                                            string childMode = "";
                                            if (eventModeDt.Select("AdverseEventId = " + eventId + " and modeid='child" + dr["modeid"] + "'").Length > 0)
                                            {
                                                childMode = eventModeDt.Select("AdverseEventId = " + eventId + " and modeid='child" + dr["modeid"] + "'")[0]["mode"].ToString();
                                            }
                                            foreach (var tempOp in tempOptions)
                                            {
                                                if (childMode.Contains(tempOp))
                                                {
                                                    value += "1@";
                                                }
                                                else
                                                {
                                                    value += "0@";
                                                }
                                            }
                                        }
                                        else if (modeDr["Options"].ToString().Contains("$0%2*"))
                                        {
                                            value += "$0%2*";
                                            string[] tempOptions = modeDr["Options"].ToString().Split(new string[] { "$0%2*" }, StringSplitOptions.None);
                                            DataRow[] tempDr = eventModeDt.Select("AdverseEventId = " + eventId + " and modeid='child" + dr["modeid"] + "'");
                                            if (tempDr.Length > 0)
                                            {
                                                value += tempDr[0]["mode"].ToString();
                                            }
                                        }
                                        else if (modeDr["Options"].ToString().Contains("$0%3*"))
                                        {
                                            value += "$0%3*";
                                            string[] tempOptions = modeDr["Options"].ToString().Split(new string[] { "$0%3*" }, StringSplitOptions.None);
                                            DataRow[] tempDr = eventModeDt.Select("AdverseEventId = " + eventId + " and modeid='child" + dr["modeid"] + "'");
                                            if (tempDr.Length > 0)
                                            {
                                                value += tempDr[0]["mode"].ToString();
                                            }
                                        }

                                        value = value.TrimEnd('@');
                                    }
                                    adverseEventModeRecord.Value = value;

                                    int modeId = AdverseEventModeRecordService.Add(adverseEventModeRecord, context);
                                    if (modeId == 0)
                                    {
                                        resultInfo.Result = 0;
                                        break;
                                    }
                                }
                            }

                            #endregion
                        }

                        if (resultInfo.Result == 1)
                        {
                            #region 添加压疮发生部位

                            DataRow[] yaChuangFsbwDrs = yaChuangFsbwDt.Select("AdverseEventId = " + eventId);
                            foreach (DataRow dr in yaChuangFsbwDrs)
                            {
                                AdverseEventYaChuangFsbw adverseEventYaChuangFsbw = new AdverseEventYaChuangFsbw();
                                adverseEventYaChuangFsbw.EventID = DataConvert.StrToInt(dr["AdverseEventId"]);
                                adverseEventYaChuangFsbw.Name = dr["typeName"].ToString();
                                adverseEventYaChuangFsbw.Remarks = dr["inputText"].ToString();
                                adverseEventYaChuangFsbw.YcLevel = dr["selectVal"].ToString();
                                adverseEventYaChuangFsbw.CreateTime = DateTime.Now;
                                int fsbwId = AdverseEventYaChuangFsbwService.Add(adverseEventYaChuangFsbw, context);
                                if (fsbwId == 0)
                                {
                                    resultInfo.Result = 0;
                                    break;
                                }
                            }

                            #endregion
                        }

                        if (resultInfo.Result == 1)
                        {
                            #region 添加根因

                            DataRow[] eventReasonDrs = eventReasonDt.Select("AdverseEventId = " + eventId);
                            foreach (DataRow dr in eventReasonDrs)
                            {
                                if (DataConvert.StrToInt(dr["reasonId"]) > 0)
                                {
                                    AdverseEventReasonRecord adverseEventReasonRecord = new AdverseEventReasonRecord();
                                    adverseEventReasonRecord.EventID = DataConvert.StrToInt(dr["AdverseEventId"]);
                                    adverseEventReasonRecord.TypeID = DataConvert.StrToInt(eventDr["EventTypeId"]);
                                    adverseEventReasonRecord.ReasonID = DataConvert.StrToInt(dr["reasonId"]);
                                    try
                                    {
                                        adverseEventReasonRecord.ParentID = DataConvert.StrToInt(reasonDt.Select("ID = " + dr["reasonId"])[0]["ParentID"]);
                                    }
                                    catch (Exception ex1)
                                    {
                                        continue; ///旧数据有一些乱七八糟的其他选项
                                    }
                                    adverseEventReasonRecord.Reason = dr["reason"].ToString();

                                    string otherReason = "";
                                    if (eventReasonDt.Select("AdverseEventId = " + eventId + " and reasonId='-100" + dr["reasonId"] + "'").Length > 0)
                                    {
                                        otherReason = eventReasonDt.Select("AdverseEventId = " + eventId + " and reasonId='-100" + dr["reasonId"] + "'")[0]["reason"].ToString();
                                    }
                                    adverseEventReasonRecord.OtherReason = otherReason;

                                    int reasonId = AdverseEventReasonRecordService.Add(adverseEventReasonRecord, context);
                                    if (reasonId == 0)
                                    {
                                        resultInfo.Result = 0;
                                        break;
                                    }
                                }
                            }

                            #endregion
                        }

                        if (resultInfo.Result == 1)
                        {
                            #region 添加当事人

                            DataRow[] eventNurseDrs = eventNurseDt.Select("AdverseEventId = " + eventId);
                            foreach (DataRow dr in eventNurseDrs)
                            {
                                AdverseEventNurse adverseEventNurse = new AdverseEventNurse();
                                adverseEventNurse.EventId = DataConvert.StrToInt(dr["AdverseEventId"]);
                                adverseEventNurse.NurseID = dr["UserId"].ToString();
                                adverseEventNurse.NurseName = dr["UserName"].ToString();
                                adverseEventNurse.NurseRank = dr["UserRank"].ToString();
                                adverseEventNurse.NurseWorkAge = dr["UserWorkAge"].ToString();
                                adverseEventNurse.NurseDepartAge = dr["UserDepartAge"].ToString();
                                adverseEventNurse.NurseAge = DataConvert.StrToInt(dr["UserAge"].ToString());
                                int nurseId = AdverseEventNurseService.Add(adverseEventNurse, context);
                                if (nurseId == 0)
                                {
                                    resultInfo.Result = 0;
                                    break;
                                }
                            }

                            #endregion
                        }

                        if (resultInfo.Result == 0)
                        {
                            break;
                        }
                    }

                    if (resultInfo.Result == 1)
                    {
                        #region 处理病人走失发生方式

                        DataRow[] eventDrs = eventDt.Select("EventTypeId = 10");
                        if (eventDrs.Length > 0)
                        {
                            string eventIds = "";
                            foreach (var item in eventDrs)
                            {
                                eventIds += item["id"].ToString() + ",";
                            }
                            eventIds = eventIds.TrimEnd(',');

                            sql = "select * from dbo.AdverseEventMode where ModeName = '走失返回时间'";
                            DataTable modeDt1 = BaseService<Model.AdverseEventMode>.GetDataTable(sql, context);

                            DataRow[] eventModeDrs1 = eventModeDt.Select("AdverseEventId in (" + eventIds + ") and modeid like 'child%'");
                            foreach (var dr in eventModeDrs1)
                            {
                                AdverseEventModeRecord adverseEventModeRecord = new AdverseEventModeRecord();
                                adverseEventModeRecord.EventID = DataConvert.StrToInt(dr["AdverseEventID"]);
                                adverseEventModeRecord.TypeID = DataConvert.StrToInt(modeDt1.Rows[0]["TypeID"]);
                                adverseEventModeRecord.ModeID = DataConvert.StrToInt(modeDt1.Rows[0]["ID"]);
                                adverseEventModeRecord.ModeName = modeDt1.Rows[0]["ModeName"].ToString();
                                adverseEventModeRecord.InputType = DataConvert.StrToInt(modeDt1.Rows[0]["InputType"]);
                                adverseEventModeRecord.Options = modeDt1.Rows[0]["Options"].ToString();
                                adverseEventModeRecord.Value = dr["mode"].ToString();

                                int modeId = AdverseEventModeRecordService.Add(adverseEventModeRecord, context);
                                if (modeId == 0)
                                {
                                    resultInfo.Result = 0;
                                    break;
                                }
                            }
                        }

                        #endregion
                    }

                    if (resultInfo.Result == 1)
                    {
                        #region 更新AdverseEventd的NurseID跟NurseName

                        sql = @"Update AdverseEvent Set NurseID=(select STUFF((select ','+NurseID from dbo.AdverseEventNurse where EventId= AdverseEvent.ID FOR XML PATH('')),1,1,''))
                                                    Where NurseID='System.String[]' and Id in (select AdverseEventId from V_AdverseEventNurse)";

                        context.Sql(sql).Execute();

                        #endregion
                    }
                    if (resultInfo.Result == 1)
                    {
                        context.Commit();
                    }
                    else
                    {
                        context.Rollback();
                    }
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return JsonDate(resultInfo);
        }

        public ActionResult UpdatePersonneltDb()
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                using (SqlConnection conn = new SqlConnection("server=172.29.102.132;database=NursingInfoSystem;user=sa;pwd=Ydhl.2018"))
                {
                    conn.Open();
                    SqlTransaction transaction;
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        //启动事务
                        transaction = conn.BeginTransaction();
                        cmd.Connection = conn;
                        cmd.Transaction = transaction;
                        try
                        {
                            DataTable dt = new DataTable();

                            cmd.CommandText = "Select Count(1) From P_PositionRecord";
                            int rowCount = DataConvert.StrToInt(cmd.ExecuteScalar());
                            if (rowCount > 0)
                            {
                                resultInfo.Result = 0;
                                resultInfo.Body = "数据已经初始化！";
                                return JsonDate(resultInfo);
                            }

                            cmd.CommandText = "select Id,UserId from P_Personnel order by id asc";
                            SqlDataReader dr = cmd.ExecuteReader();
                            dt = new DataTable();
                            dt.Load(dr);

                            foreach (DataRow item in dt.Rows)
                            {
                                int PersonnelId = DataConvert.StrToInt(item["Id"]);
                                cmd.CommandText = "select zhichengdate from [V_Personnel] where personnel_id = '" + item["UserId"] + "'";
                                string zhichengdate = DataConvert.ToString(cmd.ExecuteScalar());

                                if (!string.IsNullOrWhiteSpace(zhichengdate))
                                {
                                    string[] zhichenglist = zhichengdate.Split('|');

                                    for (int i = 0; i < zhichenglist.Length; i++)
                                    {
                                        string Position = "";
                                        switch (i)
                                        {
                                            case 0:
                                                Position = "护士";
                                                break;
                                            case 1:
                                                Position = "护师";
                                                break;
                                            case 2:
                                                Position = "主管护师";
                                                break;
                                            case 3:
                                                Position = "副主任护师";
                                                break;
                                            case 4:
                                                Position = "主任护师";
                                                break;
                                        }
                                        string zhichenginfo = zhichenglist[i];
                                        if (zhichenginfo != ",,,," && !string.IsNullOrWhiteSpace(zhichenginfo))
                                        {
                                            string GetTime = zhichenginfo.Split(',')[0];
                                            string HireTime = zhichenginfo.Split(',')[1];
                                            string ImgUrl = zhichenginfo.Split(',')[4];
                                            ImgUrl = ImgUrl.Replace("/Office/file/", "../../Upload/Personnel/File/");

                                            cmd.CommandText = "INSERT INTO [P_PositionRecord]([PersonnelId] ,[Position],[HireTime],[GetTime] ,[ImgUrl]) VALUES ('" + PersonnelId + "','" + Position + "','" + HireTime + "','" + GetTime + "','" + ImgUrl + "')";
                                            int id = cmd.ExecuteNonQuery();
                                            if (id <= 0)
                                            {
                                                resultInfo.Result = 0;
                                            }
                                        }
                                        if (resultInfo.Result == 0)
                                        {
                                            break;
                                        }
                                    }
                                }
                            }

                            if (resultInfo.Result == 1)
                            {
                                transaction.Commit(); //事务完成之后提交事务
                            }
                            else
                            {
                                transaction.Rollback();
                            }
                        }
                        catch (Exception ex)
                        {
                            //数据回滚
                            transaction.Rollback();
                            throw ex;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return JsonDate(resultInfo);
        }
    }
}